package rensoft.portfolio.client.view;

import static com.google.gwt.query.client.GQuery.$;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ScriptElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

import java.util.List;

import static cs.java.lang.Lang.doLater;
import static cs.java.lang.Lang.list;

import cs.java.lang.Run;




import rensoft.gwt.client.platform.JsInject;
import rensoft.gwt.client.view.View;

public class SocialWidget extends View<Widget> {

  private static SocialButtonsBundle bundle = GWT.create(SocialButtonsBundle.class);

  private static Binder uiBinder = GWT.create(Binder.class);

  private static native Document parseFacebook() /*-{
		$wnd.FB.XFBML.parse();
  }-*/;

  @UiField
  HTML facebook;

  @UiField
  HTML google;

  public SocialWidget() {
    initWidget(uiBinder.createAndBindUi(this));
    setVisible(false);
    doLater(1000, new Run() {
      public void run() {
        initialize();
      }
    });
  }

  private void initialize() {
    insetHTML();
    injectJavascript();
    doLater(5000, new Run() {
      public void run() {
        try {
          parseFacebook();
        } catch (JavaScriptException e) {}
        $(asWidget()).fadeIn(3000);
      }
    });
  }

  private List<ScriptElement> injectJavascript() {
    return list(JsInject.injectSrc(bundle.googleButtonJS()), JsInject.injectSrc(bundle
        .facebookButtonJS()));
  }

  private void insetHTML() {
    // twitter.setHTML(bundle.twitterButtonHTML());
    facebook.setHTML(bundle.facebookButtonHTML());
    google.setHTML(bundle.googleButtonHTML());
  }

  interface Binder extends UiBinder<Widget, SocialWidget> {}
}
